package com.liaowei.novel;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.liaowei.novel.fragment.BookshelfFragment;
import com.liaowei.novel.fragment.BookstoreFragment;
import com.liaowei.novel.fragment.CategoryFragment;
import com.liaowei.novel.fragment.HomeFragment;
import com.liaowei.novel.fragment.MineFragment;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
//    #707070
    //#5e7ce0
    private SQLiteDatabase db;
    private Fragment homeFragment,mineFragment,bookstoreFragment,categoryFragment,bookshelfFragment;
    private ImageView img_home, img_person,img_bookstore,img_category,img_bookshelf;
    private TextView tv_home, tv_person,tv_bookstore,tv_category,tv_bookshelf;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        db = SQLiteDatabase.openOrCreateDatabase("/data/data/com.liaowei.novel/databases/novel.db", null);

        this.homeFragment = new HomeFragment(db,this);
        this.mineFragment = new MineFragment(db);
        this.bookstoreFragment = new BookstoreFragment(db);
        this.categoryFragment = new CategoryFragment(db);
        this.bookshelfFragment = new BookshelfFragment(db);

        this.img_home = findViewById(R.id.img_home);
        this.img_home.setImageResource(R.mipmap.home2);
        this.img_person = findViewById(R.id.img_person);
        this.img_bookstore = findViewById(R.id.img_bookstore);
        this.img_category = findViewById(R.id.img_category);
        this.img_bookshelf = findViewById(R.id.img_bookshelf);

        this.tv_home = findViewById(R.id.tv_home);
        this.tv_person = findViewById(R.id.tv_person);
        this.tv_bookstore = findViewById(R.id.tv_bookstore);
        this.tv_category = findViewById(R.id.tv_category);
        this.tv_bookshelf = findViewById(R.id.tv_bookshelf);

        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.myframe, this.homeFragment); //默认内容
        ft.commit();

        img_home.setOnClickListener(this);
        img_person.setOnClickListener(this);
        img_bookstore.setOnClickListener(this);
        img_category.setOnClickListener(this);
        img_bookshelf.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.img_home:
                this.clear();
                img_home.setImageResource(R.mipmap.home2);
                tv_home.setTextColor(Color.parseColor("#5e7ce0"));
                FragmentTransaction ft1 = getSupportFragmentManager().beginTransaction();
                ft1.replace(R.id.myframe, this.homeFragment); //切换主页
                ft1.commit();
                break;
            case R.id.img_bookstore:
                this.clear();
                img_bookstore.setImageResource(R.mipmap.bookstore2);
                tv_bookstore.setTextColor(Color.parseColor("#5e7ce0"));
                FragmentTransaction ft2 = getSupportFragmentManager().beginTransaction();
                ft2.replace(R.id.myframe, this.bookstoreFragment); //切换书城
                ft2.commit();
                break;
            case R.id.img_category:
                this.clear();
                img_category.setImageResource(R.mipmap.category2);
                tv_category.setTextColor(Color.parseColor("#5e7ce0"));
                FragmentTransaction ft3 = getSupportFragmentManager().beginTransaction();
                ft3.replace(R.id.myframe, this.categoryFragment); //切换分类
                ft3.commit();
                break;
            case R.id.img_person:
                clear();
                img_person.setImageResource(R.mipmap.person2);
                tv_person.setTextColor(Color.parseColor("#5e7ce0"));
                FragmentTransaction ft4 = getSupportFragmentManager().beginTransaction();
                ft4.replace(R.id.myframe, this.mineFragment); // 切换个人页
                ft4.commit();
                break;
            case R.id.img_bookshelf:
                clear();
                img_bookshelf.setImageResource(R.mipmap.bookshelf2);
                tv_bookshelf.setTextColor(Color.parseColor("#5e7ce0"));
                FragmentTransaction ft5 = getSupportFragmentManager().beginTransaction();
                ft5.replace(R.id.myframe, this.bookshelfFragment); // 切换书架
                ft5.commit();
                break;
        }
    }


    // 重新设置底部导航样式
    public void clear() {
        img_person.setImageResource(R.mipmap.person1);
        img_home.setImageResource(R.mipmap.home1);
        img_bookstore.setImageResource(R.mipmap.bookstore1);
        img_category.setImageResource(R.mipmap.category1);
        img_bookshelf.setImageResource(R.mipmap.bookshelf1);

        tv_home.setTextColor(Color.BLACK);
        tv_person.setTextColor(Color.BLACK);
        tv_bookstore.setTextColor(Color.BLACK);
        tv_category.setTextColor(Color.BLACK);
        tv_bookshelf.setTextColor(Color.BLACK);
    }
}